#!/usr/bin/env python3
import random
import socket
import sys
from scapy.all import IP, TCP, Ether, get_if_hwaddr, get_if_list, sendp


def get_eth_interface():
    """获取eth0网络接口"""
    interfaces = [iface for iface in get_if_list() if "eth0" in iface]
    if not interfaces:
        print("未找到eth0接口")
        sys.exit(1)
    return interfaces[0]


def main():
    if len(sys.argv) < 3:
        print('使用方法: <目标地址> "<消息内容>"')
        sys.exit(1)

    dest_ip = socket.gethostbyname(sys.argv[1])
    iface = get_eth_interface()

    print(f"通过接口 {iface} 发送到 {dest_ip}")
    
    packet = Ether(src=get_if_hwaddr(iface), dst='ff:ff:ff:ff:ff:ff')
    packet = packet / IP(dst=dest_ip) / TCP(dport=1234, sport=random.randint(49152, 65535)) / sys.argv[2]
    
    packet.show2()
    sendp(packet, iface=iface, verbose=False)


if __name__ == '__main__':
    main()